<template>

    <div class="header">
        <div class="slogan">
            <p >在线教育平台 | 努力学习帮你走向人生巅峰</p>
        </div>
        <div class="nav">
            <ul>
                <li class="logo">
                    <router-link to="/">
                        <img src="../assets/img/head-logo.svg" alt="">
                    </router-link>
                </li>
                <li class="ele">
                    <span @click="goPage('/free')" :class="{active: url_path === '/free'}">免费课</span>
                </li>
                <li class="ele">
                    <span @click="goPage('/course')" :class="{active: url_path === '/course'}">实战课</span>
                </li>
                <li class="ele">
                    <span @click="goPage('/light-course')" :class="{active: url_path === '/light-course'}">轻课</span>
                </li>
            </ul>
            <div class="right-part">
                <div v-if="!token">
                    <span @click="put_login">登录</span>
                    <span class="line">|</span>
                    <span @click="put_register">注册</span>
                </div>
                <div v-else>
                    <span>{{ username }}</span>
                    <span class="line">|</span>
                    <span @click="logout">注销</span>
                </div>
            </div>
        </div>
        <Login v-if="is_login" @close="close_login" @go="put_register"/>
        <Register v-if="is_register" @close="close_register" @go="put_login"/>
    </div>

</template>

<script>
    import Login from "./Login";
    import Register from "./Register";

    export default {
        name: "Header",
        data() {
            return {
                // url_path: sessionStorage.url_path || '/',
                url_path: '',
                is_login: false,
                is_register: false,
                token: this.$cookies.get("token"),
                username: this.$cookies.get("username"),
            }
        },
        methods: {
            goPage(url_path) {
                // 已经是当前路由就没有必要重新跳转
                if (this.url_path !== url_path) {
                    this.$router.push(url_path);
                }
                // sessionStorage.url_path = url_path;
            },
            put_login() {
                this.is_login = true;
                this.is_register = false;
            },
            put_register() {
                this.is_login = false;
                this.is_register = true;
            },
            close_login() {
                this.is_login = false;
                this.username = this.$cookies.get("username");
                this.token = this.$cookies.get("token");
            },
            close_register() {
                this.is_register = false;
            },
            logout(){
                this.$cookies.remove("token");
                this.$cookies.remove("username");
                this.username = "";
                this.token = "";
            }
        },
        created() {
            // 把当前路径放到前台数据库中
            // sessionStorage.url_path = this.$route.path;
            // 把当前路径放到url_path中
            this.url_path = this.$route.path;
            console.log(this.url_path)
        },
        components: {
            Login,
            Register
        }
    }
</script>

<style scoped>
    .header:after {
        content: "";
        display: block;
        clear: both;
    }

    .slogan {
        background-color: #eee;
        height: 40px;
    }
    .slogan p {
        width: 1200px;
        margin: 0 auto;
        color: #aaa;
        font-size: 13px;
        line-height: 40px;
    }

    .nav {
        /*background-color: antiquewhite;*/
        user-select: none;
        width: 1200px;
        margin: 0 auto;
    }
    .nav ul {
        /*width: 1200px;*/
        /*margin: 0 auto;*/
        padding: 15px 0;
        float: left;
        margin-bottom: 0;
    }
    .nav ul:after {
        clear: both;
        content: '';
        display: block;
    }
    .nav ul li {
        float: left;
    }
    .logo {
        margin-right: 20px;
    }

    .ele {
        margin: 0 20px;
    }
    .ele span {
        display: block;
        font: 15px/36px '微软雅黑';
        border-bottom: 2px solid transparent;
        cursor: pointer;
    }
    .ele span:hover {
        border-bottom-color: orange;
    }

    .ele span.active {
        color: orange;
        border-bottom-color: orange;
    }
    .right-part {
        float: right;
    }

    .right-part .line {
        margin: 0 10px;
    }

    .right-part span {
        line-height: 68px;
        cursor: pointer;
    }
    .right-part {
        float: right;
    }

    .right-part .line {
        margin: 0 10px;
    }

    .right-part span {
        line-height: 68px;
        cursor: pointer;
    }
</style>